<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom:10px">
      <el-input
        v-model="keywords"
        placeholder="关键字"
        style="width: 200px"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search(keywords)">搜索</el-button>

    </div>
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">{{ scope.$index }}</template>
      </el-table-column>
      <el-table-column label="ID" align="center" width="65">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="计划Id" align="center" width="105">
        <template slot-scope="scope">{{ scope.row.planId }}</template>
      </el-table-column>
      <el-table-column label="计划名" align="center" width="175">
        <template slot-scope="scope">{{ scope.row.planName }}</template>
      </el-table-column>
      <el-table-column label="描述" align="center">
        <template slot-scope="scope">{{ scope.row.requirement }}</template>
      </el-table-column>
      <el-table-column label="人数" align="center" width="65">
        <template slot-scope="scope">{{ scope.row.num }}</template>
      </el-table-column>

      <el-table-column label="用户名" align="center" width="95">
        <template slot-scope="scope">{{ scope.row.userName }}</template>
      </el-table-column>

      <el-table-column label="满员状态" align="center" width="90" class-name="small-padding fixed-width">
        <template v-if="scope.row.isExamine==1?true:false" slot-scope="scope">
          <!-- 需要审核通过了才显示满员状态 -->
          <el-tag v-if="scope.row.isEnough==0" type="danger" effect="light">未满员</el-tag>
          <el-tag v-if="scope.row.isEnough==1" type="success" effect="light">已满员</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.isEnough==0?true:false" type="primary" size="mini" @click="openEdit(row.id)">
            报名
          </el-button>
          <el-button v-if="row.isEnough==1?true:false" type="primary" size="mini" @click="no()">
            不可报名
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <div style="display:flex">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :total="total"
        @current-change="currentChange"
        @size-change="sizeChange"
      />
    </div></div>
</template>

<script>

export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      size: 10

    }
  },
  // 启动自动执行该方法
  created() {
    this.fetchData()
  },
  methods: {
    openEdit(id) {
      this.$confirm('报名该实验室', '是否报名?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$router.push({ name: 'Baoming', params: { id: id }})
        this.$message({
          type: 'success',
          message: '进入报名信息页面!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    sizeChange(pageSize) {
      this.size = pageSize
      this.fetchData()
    },
    currentChange(currentPage) {
      this.page = currentPage
      this.fetchData()
    },
    async fetchData() {
      const { data: res } = await this.$axios.get('plan/getPlanListOne?page=' + this.page + '&size=' + this.size)
      this.list = res.data
      this.total = res.total
      this.$message({
        type: 'success',
        message: '查询成功!'
      })
    },

    join(id) { // 审核通过
      this.$confirm('报名该实验室', '是否报名?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.join1(id)
        this.$message({
          type: 'success',
          message: '通过成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    join1(id) { // 审核通过请求
      this.$axios.put('plan/join?id=' + id)
        .then(res => {
          this.fetchData()
        })
    },
    no() {
      this.$confirm('报名该实验室', '人员已满，不可报名！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '不可报名!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }

  }
}
</script>
